package com.QSystem.service;

import com.QSystem.mapper.SurveyMapper;
import com.QSystem.mapper.UserMapper;
import com.QSystem.pojo.Survey;
import com.QSystem.pojo.WholeSurvey;
import com.QSystem.pojo.WholeUser;
import com.QSystem.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SurveyService {
    @Autowired
    SurveyMapper surveyMapper;
    @Autowired
    UserService userService;
    @Autowired
    TitleService titleService;
    @Autowired
    ChoiceService choiceService;


    Map<String,Object> map=new HashMap<>();

    //新建问卷
    public Result createSurvey(WholeSurvey wholeSurvey){
        System.out.println("收到传来的wholesurvey："+wholeSurvey);//查看获取的信息

        Boolean exit=false;//判断问卷是否已经存在的flag
        Integer userId=userService.getUserId(wholeSurvey.getUserName());
        String surTitle=wholeSurvey.getSurvey().getSurTitle();

        wholeSurvey.getSurvey().setUserId(userId);//赋值
        Integer surId=surveyMapper.queryById(wholeSurvey.getSurvey());//查看问卷题目是否已存在

        //先判断是否已存在该问卷题目，如果存在则删除
        if(surId!=null){
            //return Result.error().message("你已经创建过该问卷啦");
            exit=true;
            surveyMapper.delete(surId);
        }
        //不存在则插入survey
        //System.out.println("插入前的survey："+wholeSurvey.getSurvey());
        surveyMapper.addSurvey(wholeSurvey.getSurvey());
        //赋值surid
        wholeSurvey.getSurvey().setSurId(surveyMapper.queryById(wholeSurvey.getSurvey()));
        titleService.updateTitles(wholeSurvey);
        if(exit==true){
            return Result.ok().message("问卷修改成功");
        }
        return Result.ok().message("问卷创建成功");

    }

    //查询某用户所有问卷
    public Result queryAll(String userName){
        Map<String,Object> maps=new HashMap<>();
        //在方法内定义，不要全局定义，否则会记录。
        WholeUser wholeUser=new WholeUser();
        //先赋值useId
        Integer userId=userService.getUserId(userName);
        wholeUser.getUser().setUserName(userName);
        wholeUser.getUser().setUserId(userId);

        //获取用户所有问卷
        List<Survey> surveys = surveyMapper.queryByName(wholeUser.getUser().getUserId());
        for (Survey survey : surveys) {
            //设置wholesurvey的信息
            WholeSurvey wholeSurvey=new WholeSurvey();
            wholeSurvey.setSurvey(survey);
            wholeSurvey.setUserName(userName);
            wholeSurvey.setUserId(userId);
            System.out.println("当前的wholesurvey"+wholeSurvey);
            //wholesurvey要获取所有的wholetitle
            titleService.queryBySurid(wholeSurvey);
            //每次遍历获取单个wholesurvey
            wholeUser.getWholeSurveys().add(wholeSurvey);
        }
        System.out.println("当前的wholeuser"+wholeUser);

        //System.out.println("根据userName查询的所有的survey"+surveyMapper.queryByName());

        //如果没有wholesurvey，则放一次进去，如果有了则直接返回
        if(maps.keySet().contains("WholeSurvey")==false){
            maps.put("WholeSurvey",wholeUser.getWholeSurveys());
        }

        return Result.ok().data(maps);
        //return  null;
    }

    //查询已发布问卷
    public Result queryRelease(String userName){
        Map<String,Object> maps=new HashMap<>();
        //在方法内定义，不要全局定义，否则会记录。
        WholeUser wholeUser=new WholeUser();
        //先赋值useId
        Integer userId=userService.getUserId(userName);
        wholeUser.getUser().setUserName(userName);
        wholeUser.getUser().setUserId(userId);

        //获取用户已发布问卷
        List<Survey> surveys = surveyMapper.queryByNameRelease(wholeUser.getUser().getUserId());
        for (Survey survey : surveys) {
            //设置wholesurvey的信息
            WholeSurvey wholeSurvey=new WholeSurvey();
            wholeSurvey.setSurvey(survey);
            wholeSurvey.setUserName(userName);
            wholeSurvey.setUserId(userId);
            System.out.println("当前的wholesurvey"+wholeSurvey);
            //wholesurvey要获取所有的wholetitle
            titleService.queryBySurid(wholeSurvey);
            //每次遍历获取单个wholesurvey
            wholeUser.getWholeSurveys().add(wholeSurvey);
        }
        System.out.println("当前的wholeuser"+wholeUser);

        //System.out.println("根据userName查询的所有的survey"+surveyMapper.queryByName());

        //如果没有wholesurvey，则放一次进去，如果有了则直接返回
        if(maps.keySet().contains("WholeSurvey")==false){
            maps.put("WholeSurvey",wholeUser.getWholeSurveys());
        }

        return Result.ok().data(maps);
        //return  null;
    }

    //查询未发布问卷
    public Result queryNotRelease(String userName){
        Map<String,Object> maps=new HashMap<>();
        //在方法内定义，不要全局定义，否则会记录。
        WholeUser wholeUser=new WholeUser();
        //先赋值useId
        Integer userId=userService.getUserId(userName);
        wholeUser.getUser().setUserName(userName);//没用到，但留着
        wholeUser.getUser().setUserId(userId);

        //获取用户未发布问卷
        List<Survey> surveys = surveyMapper.queryByNameNotRelease(wholeUser.getUser().getUserId());
        for (Survey survey : surveys) {
            //设置wholesurvey的信息
            WholeSurvey wholeSurvey=new WholeSurvey();
            wholeSurvey.setSurvey(survey);
            wholeSurvey.setUserName(userName);
            wholeSurvey.setUserId(userId);
            System.out.println("当前的wholesurvey"+wholeSurvey);
            //wholesurvey要获取所有的wholetitle
            titleService.queryBySurid(wholeSurvey);
            //每次遍历获取单个wholesurvey
            wholeUser.getWholeSurveys().add(wholeSurvey);
        }
        System.out.println("当前的wholeuser"+wholeUser);

        //System.out.println("根据userName查询的所有的survey"+surveyMapper.queryByName());

        //如果没有wholesurvey，则放一次进去，如果有了则直接返回
        if(maps.keySet().contains("WholeSurvey")==false){
            maps.put("WholeSurvey",wholeUser.getWholeSurveys());
        }

        return Result.ok().data(maps);
        //return  null;
    }


    //根据surid查询问卷(编辑问卷)
   public  Result queryById(Integer surId){
        Survey survey=new Survey();
        survey.setSurId(surId);
        //设置wholesurvey的信息
        WholeSurvey wholeSurvey=new WholeSurvey();
        wholeSurvey.setSurvey(survey);
        System.out.println("当前的wholesurvey"+wholeSurvey);
        //wholesurvey要获取所有的wholetitle
        titleService.queryBySurid(wholeSurvey);
        wholeSurvey.setSurvey(surveyMapper.queryBySurid(surId));

        //每次遍历获取单个wholesurvey
       System.out.println("queryByid的wholesurvey"+wholeSurvey);
        return Result.ok().data("WholeSurvey",wholeSurvey);
    }

    //编辑问卷(或者应该更新)
    public Result editSurvey(WholeSurvey wholeSurvey){
        System.out.println("收到传来的wholesurvey："+wholeSurvey);//查看获取的信息

        Integer delId=wholeSurvey.getSurvey().getSurId();
        surveyMapper.delete(delId);

        Boolean exit=false;//判断问卷是否已经存在的flag
        Integer userId=userService.getUserId(wholeSurvey.getUserName());
        String surTitle=wholeSurvey.getSurvey().getSurTitle();

        wholeSurvey.getSurvey().setUserId(userId);//赋值
        Integer surId=surveyMapper.queryById(wholeSurvey.getSurvey());//查看问卷题目是否已存在


            //删除原有的问卷
            surveyMapper.delete(delId);

        //插入survey
        //System.out.println("插入前的survey："+wholeSurvey.getSurvey());
        surveyMapper.addSurvey(wholeSurvey.getSurvey());
        //赋值surid
        wholeSurvey.getSurvey().setSurId(surveyMapper.queryById(wholeSurvey.getSurvey()));
        titleService.updateTitles(wholeSurvey);

            return Result.ok().message("问卷修改成功");


    }

    //根据surId删除问卷
    public  Result delSurvey(Integer surId){
        surveyMapper.delete(surId);
        return Result.ok().message("删除成功");
    }

    //根据surId将问卷状态改为发布（2）
    public  Result releaseSurvey(Integer surId){
        Integer nowState=surveyMapper.queryState(surId);
        if(nowState==2){
            return Result.error().message("该问卷已经发布过啦");
        }
        else{
            surveyMapper.release(surId);
            return Result.ok().message("发布成功");
        }
    }

    //根据surId将问卷状态改为取消发布（3）
    public  Result cancelSurvey(Integer surId){
        Integer nowState=surveyMapper.queryState(surId);
        if(nowState==2){
            surveyMapper.cancel(surId);
            return Result.ok().message("问卷已取消发布");
        }
        else{
            return Result.error().message("问卷尚未发布");
        }

    }


}
